<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button type="primary" @click="addRoleDialog">新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList" style="width: 100%" stripe border>
          <el-table-column label="序号" width="120" align="center" type="index" />
          <el-table-column prop="name" label="角色" width="240" align="center" />
          <el-table-column prop="description" label="描述" align="center" />
          <el-table-column label="操作" width="340" fixed="right" align="center">
            <template #default="{ row }">
              <el-button size="small" type="success" @click="assignPermission(row.id)">分配权限</el-button>
              <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            :current-page="query.page"
            :page-size="query.pagesize"
            layout="prev, pager, next"
            :total="total"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增组件 -->
    <add-role-vue
      ref="addRole"
      :dialog-visible="dialogVisible"
      @close-dialog="closeDialog"
      @update-list="getRoleLIst"
    />
    <!-- 分配权限的弹层 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible="showAssignDialog"
      @close="closeAssignDialog"
      @open="getPermissionList"
    >
      <el-tree
        ref="permTree"
        :data="permissionList"
        :props="{ label: 'name' }"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <!-- 权限点数据展示 -->
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeAssignDialog">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleListAPI, delRoleAPI, assignPermission, getRoleDetailAPI } from '@/api/setting'
import { getPermissionList } from '@/api/Permission'
import transTree from '@/utils/transTree'
import addRoleVue from './components/AddRole.vue'
export default {
  name: 'Setting',
  components: {
    addRoleVue
  },
  data() {
    return {
      roleList: [],
      query: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      dialogVisible: false,
      showAssignDialog: false,
      permissionList: [],
      curId: null
    }
  },
  mounted() {
    this.getRoleLIst()
  },
  methods: {
    // 获取角色列表
    async getRoleLIst() {
      const res = await getRoleListAPI(this.query)
      // console.log(res)
      this.roleList = res.rows
      this.total = res.total
    },
    // 获取当前页码重新赋值给page数据项
    pageChange(page) {
      this.query.page = page
      this.getRoleLIst()
    },
    addRoleDialog() {
      // 显示弹框
      this.dialogVisible = true
    },
    // 点击x关闭弹窗，重置
    closeDialog() {
      this.dialogVisible = false
    },
    // 数据回填
    editRole(id) {
      this.dialogVisible = true
      this.$refs.addRole.getRoleDetail(id)
    },
    // 删除角色
    delRole(id) {
      this.$confirm('确定要删除此角色吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleAPI(id)
        this.getRoleLIst()
        this.$message.success('删除成功')
      }).catch(() => {

      })
    },
    async assignPermission(id) {
      this.showAssignDialog = true
      this.curId = id
      const res = await getRoleDetailAPI(id)
      this.$refs.permTree.setCheckedKeys(res.permIds)
    },
    closeAssignDialog() {
      this.showAssignDialog = false
    },
    // 获取权限列表
    async getPermissionList() {
      const res = await getPermissionList()
      this.permissionList = transTree(res)
    },
    async confirm() {
      // permIds: 由我们选中项的权限id组成的数组  Tree组件中的方法getCheckedKeys()
      const ids = this.$refs.permTree.getCheckedKeys()
      await assignPermission({ id: this.curId, permIds: ids })
      this.$message.success('分配成功')
      this.showAssignDialog = false
    }
  }
}
</script>
